package com.promis.widget.imgzoom;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.promis.util.ScreenUtils;
import com.promis.util.StringUtils;
import com.promis.widget.R;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

//import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by jidanbo on 2018/6/6.
 */

public class ThumbnailView extends android.support.v7.widget.AppCompatImageView {

    private boolean square = false;
    private Drawable resource;

    public ThumbnailView(Context context) {
        super(context);
        init();
    }

    public ThumbnailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ThumbnailView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (square)
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        else
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public void setSquare(boolean square) {
        this.square = square;
    }

    public ThumbnailRect getOriginalRect() {
        int[] outLocation = new int[2];
        getLocationInWindow(outLocation);
        return new ThumbnailRect(outLocation[0],outLocation[1], getWidth(), getHeight());
    }

    public Drawable getResource() {
        return resource;
    }

    @Override
    public void setImageDrawable(@Nullable Drawable drawable) {
        super.setImageDrawable(drawable);
        this.resource = drawable;
    }

    public void setImageUrl(String imageUrl) {

//        if (!StringUtils.isNullOrEmpty(imageUrl)) {
//            Glide.with(getContext()).load(imageUrl).dontAnimate().into(new ImageViewTarget(this) {
//                @Override
//                protected void setResource(GlideDrawable resource) {
//                    if (!square) {
//                        int width = resource.getIntrinsicWidth();
//                        int height = resource.getIntrinsicHeight();
//
//                        int sWidth = 0;
//                        int sHeigth = 0;
//                        if (width > height) {
//                            sWidth = ScreenUtils.getScreenRect(getContext()).width() / 5 * 3;
//                            sHeigth = (int) (((float) sWidth) / width * height);
//                        } else if (width < height) {
//                            sWidth = ScreenUtils.getScreenRect(getContext()).width() / 5 * 2;
//                            sHeigth = (int) (((float) sWidth) / width * height);
//                        } else {
//                            sWidth = ScreenUtils.getScreenRect(getContext()).width() / 5 * 3;
//                            sHeigth = sWidth;
//                        }
//                        getLayoutParams().width = sWidth;
//                        getLayoutParams().height = sHeigth;
//                    }
//
//                    setImageDrawable(resource);
//                }
//            });
//        } else {
//            getLayoutParams().width = ScreenUtils.getScreenRect(getContext()).width() / 2;
//            getLayoutParams().height = getLayoutParams().width;
//            setImageResource(R.color.bg_transparent);
//        }

//        RequestOptions options = new RequestOptions().centerCrop();
//        Glide.with(getContext()).load(imageUrl).apply(options).into(this);

        if (!StringUtils.isNullOrEmpty(imageUrl)) {
            RequestOptions requestOptions = RequestOptions.bitmapTransform(new MultiTransformation(
                    new CenterCrop(),
                    new RoundedCornersTransformation(ScreenUtils.dp2px(getContext(), 2), 0, RoundedCornersTransformation.CornerType.ALL)))
                    .error(R.color.bg_gray).placeholder(R.color.bg_gray);
            Glide.with(getContext()).load(imageUrl).apply(requestOptions).into(this);
        }

//        Glide.with(getContext()).load(imageUrl).transform(new GlideRoundTransform(getContext(), 12)).into(new ImageViewTarget(this));
//        Glide.with(getContext()).load(imageUrl).into(new ImageViewTarget(this));
    }
}
